package com.czk.polling;

import com.czk.ServerIps;

/**
 * 轮询的随机算法
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/22 15:02
 */
public class RoundRobin {
    private static Integer pos = 0;

    public static void main(String[] args) {
        for (int i = 0; i < 15; i++) {
            System.out.println(getServer());
        }
    }

    public static String getServer() {
        String ip = null;
        synchronized (pos) {
            if (pos >= ServerIps.LIST.size()) {
                pos = 0;
            }
            ip = ServerIps.LIST.get(pos);
            pos++;
        }
        return ip;
    }
}
